package com.shujia.scala

import scala.collection.immutable
import scala.io.Source

object Demo31Student {
  def main(args: Array[String]): Unit = {
    /**
     * 4、统计偏科最严重的前100名学生
     *
     * 偏科评估的标准： 方差
     */

    //1、读取分数
    val lines: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //2、切分数据
    val scoreArr: List[Array[String]] = lines.map(line => line.split(","))

    //3、过滤脏数据
    val scoreFilter: List[Array[String]] = scoreArr.filter(arr => arr.length == 3)

    //4、取出学号和分数
    val idAndScore: List[(String, Int)] = scoreFilter.map {
      case Array(id: String, _, sco: String) =>
        (id, sco.toInt)
    }

    //5、按照学号分组
    val groupBy: Map[String, List[(String, Int)]] = idAndScore.groupBy(kv => kv._1)

    //计算方差
    val std: List[(String, Double, List[(String, Int)])] = groupBy.map {
      case (id: String, list: List[(String, Int)]) =>
        //一个学生所有的分数
        val scores: List[Int] = list.map { case (_, sco: Int) => sco }

        /**
         * 计算方差
         * 1、计算总数
         * 2、计算平均值
         * 3、计算方差
         *
         */
        //科目数
        val N: Double = scores.length.toDouble
        //平均数
        val avg: Double = scores.sum / N

        //计算方差
        val std: Double = scores.map((sco: Int) => (sco - avg) * (sco - avg)).sum / N

        (id, std, list)
    }.toList

    //按照方差排序，取前100
    val sortByStd: List[(String, Double, List[(String, Int)])] = std.sortBy(kv => -kv._2)


    //取前100
    val top10: List[(String, Double, List[(String, Int)])] = sortByStd.take(100)


    top10.foreach(println)
  }

}
